<!--
 * @Author: White
 * @description: 推荐MV
 * @LastEditTime: 2021-08-26 11:07:39
-->

<template>
  <div class="mv" :style="{ 'color': theme.text }">
    <div class="mv-title">
      <h3>推荐MV</h3>
      <i class="el-icon-arrow-right"></i>
    </div>
    <div class="mv-content">
      <div class="mv-item" v-for="(item, index) in list" :key="index" @click="playMv(item)">
        <img :src="item.picUrl" alt />
        <span>
          <i class="el-icon-caret-right"></i>
          {{ item.playCount }}
        </span>
        <p class="name">{{ item.name }}</p>
        <p class="author">{{ item.artistName }}</p>
      </div>

      <!-- 播放弹窗 -->
      <el-dialog :title="dialogTableVisibletitle" v-model="dialogTableVisible" :before-close="handleClose">
        <video ref="video" width="658" :src="dialogSrc" controls autoplay="autoplay" style="width: 100%"></video>
      </el-dialog>
    </div>
  </div>
</template>
  
<script>
import { useStore } from 'vuex'
import { reactive, onMounted, computed, toRefs, ref } from 'vue'
import { getMv, getMvPlay } from '@/api/music'

export default {
  setup() {
    const store = useStore()
    const video = ref(null)
    const theme = computed(() => store.state.theme)
    const state = reactive({
      dialogTableVisible: false,
      dialogTableVisibletitle: '',
      dialogSrc: '',
      list: []
    })
    onMounted(async () => {
      const res = await getMv()
      state.list = res.data.result.splice(0, 4)
    })

    // 播放mv
    async function playMv(item) {
      state.dialogTableVisible = true
      state.dialogTableVisibletitle = item.name

      let parmas = { id: item.id }
      const data = await getMvPlay(parmas)
      state.dialogSrc = data.data.data.url
    }

    // 关闭窗口
    function handleClose (){
      let videoDom = video.value
      videoDom.pause()
      state.dialogTableVisible = false
    }

    return {
      ...toRefs(state),
      video,
      handleClose,
      playMv,
      theme
    }
  }
}
</script>
  
<style lang="scss" scoped>
.mv {
  margin: 10px 0px;
  .mv-title {
    height: 30px;
    display: flex;
    align-items: center;
    h3 {
      line-height: 30px;
      font-size: 16px;
      font-weight: 700;
      text-decoration: none;
    }
  }
  .mv-content {
    display: flex;
    flex-wrap: wrap;
    justify-content: space-between;
    margin-bottom: 10px;
    height: 150px;
    .mv-item {
      width: calc(25% - 15px);
      height: 100px;
      margin-right: 10px;
      position: relative;
      img {
        width: 100%;
        height: 100px;
        border-radius: 6px;
      }
      span {
        position: absolute;
        top: 4px;
        right: 4px;
        color: #fff;
        font-size: 12px;
        font-weight: 700;
      }
      .name {
        margin: 4px 0 2px;
        font-size: 14px;
        font-weight: 700;
        width: 100%;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
      }
      .author {
        display: flex;
        align-items: center;
        font-size: 13px;
        color: #999;
      }
    }
  }
}
</style>
